package com.duobaoyu.dby.supply.admin.controller.refund;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.duobaoyu.dby.channel.adapter.enums.OperationTypeEnum;
import com.duobaoyu.dby.common.core.util.R;
import com.duobaoyu.dby.supply.admin.annotation.SupplySysLog;
import com.duobaoyu.dby.supply.admin.service.AdminAfterSaleBizService;
import com.duobaoyu.dby.supply.common.biz.dto.refund.*;
import com.duobaoyu.dby.supply.common.biz.vo.refund.AfterSaleListVO;
import com.duobaoyu.dby.supply.common.biz.vo.refund.AfterSaleVO;
import com.duobaoyu.dby.supply.common.biz.vo.refund.ReasonVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 售后
 *
 * @author jz
 * @version 1.0
 * @date 2022/6/5 17:47
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/admin/after-sale")
@Api(value = "after-sale", tags = "售后")
public class AfterSaleController {

    private final AdminAfterSaleBizService adminAfterSaleBizService;

    /**
     * 售后订单列表
     *
     * @param afterSaleApplyQuery afterSaleApplyQueryDTO
     * @return R<Page < AfterSaleApplyVO>>
     */
    @GetMapping("/page")
    @ApiOperation(value = "售后订单列表", notes = "售后订单列表")
    @SupplySysLog(value = "售后订单列表", operationType = OperationTypeEnum.SELECT)
    public R<Page<AfterSaleListVO>> queryPage(AfterSaleApplyQueryDTO afterSaleApplyQuery) {
        return R.ok(adminAfterSaleBizService.queryPage(afterSaleApplyQuery));
    }

    /**
     * 获取售后详情
     *
     * @return
     */
    @GetMapping("/{afterSaleSn}")
    @ApiOperation(value = "获取售后详情", notes = "获取售后详情")
    @SupplySysLog(value = "获取售后详情", operationType = OperationTypeEnum.SELECT)
    public R<AfterSaleVO> afterSaleApplyDetail(@PathVariable("afterSaleSn") String afterSaleSn) {
        return R.ok(adminAfterSaleBizService.afterSaleDetail(afterSaleSn));
    }

    /**
     * 售后记录审核
     */
    @PostMapping("/agree")
    @ApiOperation(value = "同意售后申请", notes = "同意售后申请")
    @SupplySysLog(value = "同意售后申请", operationType = OperationTypeEnum.UPDATE)
    public R<Boolean> agreeAfterSaleApply(@RequestBody AfterSaleChannelAddressDTO afterSaleChannelAddress) {
        return R.ok(adminAfterSaleBizService.agreeAfterSale(afterSaleChannelAddress));
    }

    /**
     * 售后记录审核
     */
    @PostMapping("/reject/{afterSaleSn}")
    @ApiOperation(value = "拒绝售后申请", notes = "拒绝售后申请")
    @SupplySysLog(value = "拒绝售后申请", operationType = OperationTypeEnum.UPDATE)
    public R<Boolean> rejectAfterSale(@PathVariable("afterSaleSn") String afterSaleSn,
                                      @RequestBody AfterSaleRejectReasonDTO rejectReasonDTO) {
        return R.ok(adminAfterSaleBizService.rejectAfterSale(afterSaleSn, rejectReasonDTO.getRejectReason()));
    }

    /**
     * 修改售后单信息
     *
     * @param afterSaleUpdate
     * @return
     */
    @PostMapping("/modifyAfterSale")
    @ApiOperation(value = "修改售后单信息", notes = "修改售后单信息")
    @SupplySysLog(value = "修改售后单信息", operationType = OperationTypeEnum.UPDATE)
    public R<Boolean> modifyAfterSale(@Validated @RequestBody AfterSaleUpdateDTO afterSaleUpdate) {
        return R.ok(adminAfterSaleBizService.modifyAfterSale(afterSaleUpdate));
    }


    @PostMapping("/afterSaleFixData")
    @SupplySysLog(value = "订正售后单信息", operationType = OperationTypeEnum.UPDATE)
    public R<Boolean> afterSaleFixData(@Validated @RequestBody AfterSaleFixDTO dto) {
        return R.ok(adminAfterSaleBizService.fixData(dto));
    }

    /**
     * 支持所有修改信息
     *
     * @param afterSaleUpdate
     * @return
     */
    @PutMapping("/modifyAllAfterSale")
    @ApiOperation(value = "支持所有修改信息", notes = "支持所有修改信息")
    @SupplySysLog(value = "售后单所有修改信息", operationType = OperationTypeEnum.UPDATE)
    public R<Boolean> modifyAllAfterSale(@Validated @RequestBody AfterSaleUpdateDTO afterSaleUpdate) {
        return R.ok(adminAfterSaleBizService.modifyAfterSale(afterSaleUpdate));
    }

    /**
     * 同意退款
     *
     * @param afterSaleSn
     * @return
     */
    @PutMapping("/refund/agree/{afterSaleSn}")
    @ApiOperation(value = "同意退款", notes = "同意退款")
    @SupplySysLog(value = "修改售后单信息", operationType = OperationTypeEnum.UPDATE)
    public R<Boolean> agreeRefund(@PathVariable("afterSaleSn") String afterSaleSn,
                                  @RequestBody AfterSaleRefundDTO afterSaleRefundDTO) {
        return R.ok(adminAfterSaleBizService.agreeRefund(afterSaleSn,
                afterSaleRefundDTO.getRefundAmount(), afterSaleRefundDTO.getRefundFreightAmount()));
    }

    /**
     * 批量导出
     * 售后订单批量导出
     *
     * @param afterSaleApplyQuery
     * @return
     */
    @ApiOperation(value = "导出售后订单列表", notes = "导出售后订单列表")
    @SupplySysLog(value = "导出售后订单列表", operationType = OperationTypeEnum.SELECT)
    @PostMapping(value = "/export")
    public R<Boolean> exportExcel(@RequestBody AfterSaleApplyQueryDTO afterSaleApplyQuery) {
        return R.ok(adminAfterSaleBizService.afterSaleExport(afterSaleApplyQuery));
    }


    /**
     * 批量查询商品售后权益
     *
     * @return
     */
    @ApiOperation("批量查询商品售后权益")
    @GetMapping("/attributes/{afterSaleSn}")
    public R<List<ReasonVO>> queryAfterSaleAttributes(@PathVariable String afterSaleSn) {
        return R.ok(adminAfterSaleBizService.queryAfterSaleAttributes(afterSaleSn));
    }


	@ApiOperation("获取代审核数量")
    @GetMapping("/wait-review")
    public R<Long> waitReview(){
    	return R.ok(adminAfterSaleBizService.waitReview());
	}

}
